package weaver.interfaces.message.kqmsg;

import com.cloudstore.dev.api.bean.MessageBean;
import com.cloudstore.dev.api.bean.MessageType;
import com.cloudstore.dev.api.util.Util_Message;
import com.engine.kq.biz.KQWorkTime;
import org.apache.commons.lang3.StringUtils;
import weaver.common.DateUtil;
import weaver.common.StringUtil;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.interfaces.schedule.BaseCronJob;

import java.io.IOException;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 考勤异常消息生成定时任务
 */
public class KqMessageJob extends BaseCronJob {

    BaseBean baseBean= new BaseBean();

    @Override
    public void execute() {
        baseBean.writeLog("KqMessageJob start.");
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm");
        RecordSet recordSet= new RecordSet();
        Map<String,Object> resources= new HashMap<>();
        String today = DateUtil.getDate(new Date());
        KQWorkTime kqWorkTime = new KQWorkTime();
        String queryExKqSql="select resourceid,workbegindate+' '+workbegintime as workbegin,workenddate+' '+workendtime as workend,signindate+' '+signintime as signin,signoutdate+' '+signouttime as signout, belatemins, leaveearlymins, " +
                " absenteeismmins, forgotcheckMins from kq_format_detail where resourceid = ? and kqdate = ? ";
        try {
            String queryResSql="select id from hrmresource where status in (0,1,2,3)";
            recordSet.executeQuery(queryResSql);
            while (recordSet.next()) {
                resources.put(Util.null2String(recordSet.getString("id")),today);
            }
//            String resourceId = "71";
            for (String resourceId: resources.keySet()){
                String day = Util.null2String(resources.get(resourceId));
                boolean workDay = kqWorkTime.isWorkDay(resourceId, day);
                if (workDay){
                    recordSet.executeQuery(queryExKqSql,resourceId,day);
                    if (recordSet.next()) {
                        String resid= Util.null2String(recordSet.getString("resourceid"));
                        String workbeginStr= Util.null2String(recordSet.getString("workbegin"));
                        String workendStr= Util.null2String(recordSet.getString("workend"));
                        String signinStr= Util.null2String(recordSet.getString("signin"));
                        String signoutStr= Util.null2String(recordSet.getString("signout"));
                        String belatemins= Util.null2String(recordSet.getString("belatemins"));
                        if (StringUtils.isEmpty(belatemins)) {
                            belatemins = "0";
                        }
                        String leaveearlymins= Util.null2String(recordSet.getString("leaveearlymins"));
                        if (StringUtils.isEmpty(leaveearlymins)) {
                            leaveearlymins = "0";
                        }
                        String absenteeismmins= Util.null2String(recordSet.getString("absenteeismmins"));
                        if (StringUtils.isEmpty(absenteeismmins)) {
                            absenteeismmins = "0";
                        }
                        String forgotcheckMins= Util.null2String(recordSet.getString("forgotcheckMins"));
                        if (StringUtils.isEmpty(forgotcheckMins)) {
                            forgotcheckMins = "0";
                        }
                        if (new BigDecimal(belatemins).compareTo(BigDecimal.ZERO) > 0 || new BigDecimal(leaveearlymins).compareTo(BigDecimal.ZERO) > 0 ||
                                new BigDecimal(absenteeismmins).compareTo(BigDecimal.ZERO) > 0 || new BigDecimal(forgotcheckMins).compareTo(BigDecimal.ZERO) > 0) {
                            String context="今日考勤异常，签到签退如下：" + signinStr + "、" + signoutStr;
                            MessageBean message = createMessage(context, resid);
                            Util_Message.store(message);
                        }
                    }
                }
            }
            baseBean.writeLog("KqMessageJob end.");
        } catch (Exception e) {
            baseBean.writeLog("KqMessageJob error:" + e.getMessage());
        }
    }

    public MessageBean createMessage(String context, String resourceId) throws IOException {
        String code = baseBean.getPropValue("propExtends", "kqmessagetypecode");
        MessageType messageType = MessageType.newInstance(Util.getIntValue(code));
        String title = "今日考勤异常提醒"; //标题
        Set<String> userIdList = new HashSet<>();
        userIdList.add(resourceId);
        MessageBean message=Util_Message.createMessage(messageType,userIdList, title, context, "", "");

        Integer creater=0;
        String id="";
        String queryMaSql="select top(1) id from hrmresourcemanager order by id ";
        RecordSet recordSet = new RecordSet();
        recordSet.executeQuery(queryMaSql);
        while (recordSet.next()){
            id = Util.null2String(recordSet.getString("id"));
        }
        if (StringUtil.isNotNull(id)){
            creater=Integer.parseInt(id);
            message.setCreater(creater);
        }else {
            message.setCreater(1);
        }

        return message;
    }
}
